//复制
export  function uniCopy(content) {
	if(!content) 
		return
		 // error('复制的内容不能为空 !')
		content = typeof content === 'string' ? content : content.toString() // 复制内容，必须字符串，数字需要转换为字符串
	/**
	 * 小程序端 和 app端的复制逻辑-----h5会导致键盘弹出
	 */
	// uni.setClipboardData({
	// 	data: content,
	// 	success: function() {
	// 		uni.showToast({
	// 			title:'复制成功',
	// 			icon: 'none'
	// 		})
	// 	},
	// 	fail:function(){
	// 		uni.showToast({
	// 			title: '复制失败',
	// 			icon: 'none'
	// 		})
	// 	}
	// });
	
	/**
	 * H5端的复制逻辑
	 */
	if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
		// 不支持
		uni.showToast({
			title: '浏览器不支持',
			icon: 'none',
			duration: 3000,
		})
	}
	let textarea = document.createElement("textarea")
	textarea.value = content
	textarea.readOnly = "readOnly"
	document.body.appendChild(textarea)
	textarea.select() // 选择对象
	uni.hideKeyboard()
	textarea.setSelectionRange(0, content.length) //核心
	let result = document.execCommand("copy") // 执行浏览器复制命令
	if(result){
		uni.showToast({
			title: '复制成功',
			icon: 'none'
		})
	}else{
		uni.showToast({
			title: '浏览器不支持--h5',
			icon: 'none',
			duration: 3000,
		})
	}	
	textarea.remove()
}

export function formatTime(time,format="YYYY-MM-DD HH:MM:SS"){
	  if(!time) return '-'
	  let length = Number(time).toString().length
	  if (length==0) return time
	  if(length==10)
		time*=1000
	  let date = new Date(time)
	  let year  = date.getFullYear()
	  let month  = getTwoStr(date.getMonth()+1)
	  let day = getTwoStr(date.getDate())
	  let hours = getTwoStr( date.getHours())
	  let minutes = getTwoStr( date.getMinutes())
	  let second = getTwoStr( date.getSeconds())
	  switch(format){
		  case 'YYYY-MM-DD HH:MM:SS':return year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+second
		  case 'YYYY-MM-DD HH:MM':return year+'-'+month+'-'+day+' '+hours+':'+minutes
		  case 'MM-DD HH:MM':return month+'-'+day+' '+hours+':'+minutes
		  case 'YYYY-MM-DD':return year+'-'+month+'-'+day
		  case 'YYYYMMDD':return year+''+month+''+day
		  case 'MM-DD HH:MM:SS':return month+'-'+day+' '+hours+':'+minutes+':'+second
		  case 'timestamp': return {day:day,hours:hours,minutes:minutes,second:second}
		  default:return year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+second
	  }
}

export function getTwoStr(time){
  return time>=10?time:'0'+time
}

export function setStorage(key,data,expr = 10800){
	data.time = Math.floor(new Date()/1000)+expr
	uni.setStorageSync({key:'data', data:JSON.stringify(res)})
}

export function getStorage(key){
	let value = uni.getStorageSync(key) || null
	if(value != null)
		return JSON.parse(value)
	return null
}

export function getUrlParams() {
    var searchHref = window.location.search.replace('?', '');
    var params = searchHref.split('&');
    var returnParam = {};
    params.forEach(function(param) {
      var paramSplit = param.split('=');
      returnParam[paramSplit[0]] = paramSplit[1];
    });
    return returnParam;
  }
  
export function checkHostOrImage(url){
	//非常规化域名 不以.com 结尾 检查 
	// 链接中是否存在png\jpeg\jpg
	let imageVal=-1
	if(url.indexOf('.com/')>-1){
		imageVal = url.length - (url.indexOf('.com/')+4)
	}else if(url.indexOf('.com')>-1){
		imageVal = url.length - (url.indexOf('.com')+4)
	}else if(url.indexOf('http')>-1 && (url.indexOf('.png')>-1 || url.indexOf('.jpg')>-1 || url.indexOf('.jpeg')>-1)){
		imageVal = 2
	}
	return imageVal>1
  }

/*时间戳转化年月日*/
export function timeStampToString(val,setting){

	let dateLong = parseInt(val);
	let myDate = new Date(dateLong* 1000);
	let myMonth = myDate.getMonth() + 1;
	if(myMonth < 10){
		myMonth = '0'+ myMonth;  //补齐
	}
	let mydate = myDate.getDate();
	if(myDate.getDate()<10){
		mydate = '0'+ myDate.getDate();  //补齐
	}

	let hours = myDate.getHours();
	let minutes = myDate.getMinutes();
	let seconds = myDate.getSeconds();
	hours = 10>hours ?'0' +hours: hours+'';
	minutes = 10>minutes?':0' +minutes:':'+minutes+'';
	seconds= 10>seconds?':0' +seconds:':'+seconds+'';
	if(!setting.showHour){
		hours = ''
	}
	if(!setting.showMin){
		minutes = ''
	}
	if(!setting.showSec){
		seconds = ''
	}
	let day = ''
	if(setting.showWeek){
		let week = myDate.getDay();

		let weeks = ["日","一","二","三","四","五","六"];

		let getWeek = "星期" + weeks[week]
		day = myDate.getFullYear() + '-' + myMonth + '-' + mydate + ' ' + getWeek +' '+ hours + minutes + seconds;
	}else{
		day = myDate.getFullYear() + '-' + myMonth + '-' + mydate + ' ' + hours + minutes + seconds;
	}
	let dateChoose = day
	return dateChoose
}



/*文字转义*/
export function strChange(val,num){
	let text = val.split('{number}')
	let newTxt = text[0] + num + text[1] +''
	return newTxt
}
//字符串转化为小写
 export function strToLC(str=''){
	 let res = str.toLowerCase()
	 return res
 }
 //检查文件类型
 export function checkType(str) {
	if(!str){
		return 'file'
	}
	let type = str.toLowerCase() 
	if (type.indexOf('.jpg') > -1 || type.indexOf('.jpeg') > -1 || type.indexOf('.png') > -1 || type.indexOf('.gif') > -1) {
		return 'image'
	}
	else if (type.indexOf('.mp4') > -1 || type.indexOf('.AVI') > -1 || type.indexOf('.mov') > -1 || type.indexOf( '.rmvb') > -1 || type.indexOf('.FLV') > -1 || type.indexOf('.rm') > -1 || type.indexOf('.3GP') > - 1) {
		return 'video'
	}else{
		return 'file'
	}
}
